package com.mstore.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.SessionAware;

import com.mstore.pojo.Account;
import com.mstore.pojo.OrderInfo;
import com.mstore.pojo.Tariff;
import com.mstore.service.AccountService;
import com.mstore.service.BaseMemberService;
import com.mstore.service.OrderInfoService;
import com.mstore.service.ShopService;
import com.mstore.service.TariffService;

public class OrderInfoAction extends BaseAction implements SessionAware {

	private static final long serialVersionUID = 5827769086657112476L;

	private OrderInfoService orderInfoService;

	private List<OrderInfo> orderInfos;

	private Map<String, Object> session;

	private String result;

	private OrderInfo orderInfo;

	private Tariff tariff;

	private TariffService tariffService;

	private ShopService shopService;

	private AccountService accountService;

	private BaseMemberService baseMemberService;

	/**
	 * 查询企业的订单
	 */
	public String watchMyOrderInfo() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;
		orderInfos = orderInfoService.watchMyOrderInfo(account.getId());

		return "watchMyOrderInfo";
	}

	/**
	 * 下订单
	 */
	public String addMyOrderInfo() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;

		if (tariff == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}

		Long tariffId = tariff.getTariffId();

		if (tariffId == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}

		tariff = tariffService.watchTariffById(tariffId);

		if (tariff == null) {
			result = "资费信息不存在";
			return "error";
		}

		// 下新订单
		orderInfo = new OrderInfo();
		orderInfo.setTariff(tariff);
		orderInfo.setAccount(account);
		orderInfo.setGoods(tariff.getGoods());
		orderInfo.setGoodsName(orderInfo.getGoods() != null ? orderInfo
				.getGoods().getGoodsName() : "");
		orderInfo.setTradeAmount(tariff.getFee());
		orderInfo.setStatus(0);
		orderInfo.setCreateDate(getCreateTime());

		int validDays = tariff.getValidDays();
		if (validDays == 0) {
			// 计数商品
			orderInfo.setAmount(tariff.getAmount());
			orderInfo.setRemain(tariff.getAmount());
		}
		else {
			// 计时商品
			// 获得当前时间（毫秒）
			Long currentTimeMillis = System.currentTimeMillis();
			// 将有效时间转换成毫秒
			Long validDaysMillis = Long.valueOf(validDays * 3600000 + "");
			// 得到失效时间（毫秒）
			Long endDateMillis = currentTimeMillis + validDaysMillis;
			orderInfo.setBeginDate(getCreateTime());
			orderInfo.setEndDate(getCreateTime(endDateMillis));
		}

		orderInfoService.addMyOrderInfo(orderInfo);

		return "addMyOrderInfoSuccess";
	}

	/**
	 * 删除订单
	 */
	public String delMyOrderInfo() {
		if (orderInfo == null) {
			result = "系统数据异常";
			return "error";
		}

		if (StringUtils.isBlank(orderInfo.getOrderId())) {
			result = "系统数据异常";
			return "error";
		}

		orderInfoService.delOrderInfo(orderInfo);

		return "delSuccess";
	}

	public OrderInfoService getOrderInfoService() {
		return orderInfoService;
	}

	public void setOrderInfoService(OrderInfoService orderInfoService) {
		this.orderInfoService = orderInfoService;
	}

	public List<OrderInfo> getOrderInfos() {
		return orderInfos;
	}

	public void setOrderInfos(List<OrderInfo> orderInfos) {
		this.orderInfos = orderInfos;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public OrderInfo getOrderInfo() {
		return orderInfo;
	}

	public void setOrderInfo(OrderInfo orderInfo) {
		this.orderInfo = orderInfo;
	}

	public Tariff getTariff() {
		return tariff;
	}

	public void setTariff(Tariff tariff) {
		this.tariff = tariff;
	}

	public TariffService getTariffService() {
		return tariffService;
	}

	public void setTariffService(TariffService tariffService) {
		this.tariffService = tariffService;
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public AccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public BaseMemberService getBaseMemberService() {
		return baseMemberService;
	}

	public void setBaseMemberService(BaseMemberService baseMemberService) {
		this.baseMemberService = baseMemberService;
	}

	@Override
	public void setSession(Map<String, Object> map) {
		this.session = map;
	}

}
